<?php

namespace app\admin\controller;


use app\admin\model\Information;
use app\admin\model\InformationClassification;
use app\admin\model\InformationKeyword;
use app\admin\model\Substation;

/**
 * Author:Jack Wang
 * Time:2018-10-23 09:15:07
 * Describe:资讯控制器
 * Class Informations
 * @package app\admin\controller
 */
class Informations extends Base
{

    //资讯分类列表
    public function classificationList(){
        $keyword = input("keyword/s");
        if(input("?table")) {
            $InformationClassification = new InformationClassification();
            $result["code"] = 0;
            $result["data"] = $InformationClassification->classificationList($keyword);
            $result["data"] = collection($result["data"])->toArray();
            if(empty($keyword)) {//搜索不保持树结构
                $result["data"] = Menus::arrayToTree($result["data"],0,0,"classification_id");
            }
            return json($result);
        }
        return view('classificationList',[
            'title'         =>  '资讯分类列表',
        ]);
    }

    //添加修改资讯分类
    public function classificationAdd(){
        $classification_id = input("classification_id/d",0);
        $InformationClassification = new InformationClassification();
        if(request()->isPost()){
            $data = input("post.");
            $msg = $InformationClassification->checkData($data, [
                ["classification_name", "require", "分类名称不能为空"],
                ["substation_id", "require", "请选择发布分站"],
            ]);
            //验证是否有权限修改
            if ($msg === true) {
                //验证是否可以修改
                if($InformationClassification->checkName($data["classification_name"],$classification_id,$data["substation_id"])){
                    $result["code"] = 1;
                    $result["msg"] = '分类名称已存在';
                }else {
                    if($InformationClassification->checkParentId($data["parent_id"],$data["substation_id"])) {
                        if($InformationClassification->checkSave($classification_id,$data["substation_id"])) {
                            if ($InformationClassification->mySave($data,"classification_id", $classification_id)) {
                                if (!empty($classification_id)) {
                                    //修改资讯表分类名称
                                    $Information = new Information();
                                    $Information->setFieldValue("classification_id", $classification_id, "classification_name", $data["classification_name"]);
                                }
                                $result["code"] = 0;
                                $result["msg"] = "操作成功！";
                            } else {
                                $result["code"] = 10000;
                                $result["msg"] = "操作失败！";
                            }
                        }else{
                            $result["code"] = 20000;
                            $result["msg"] = "没有权限操作该网站的内容!";
                        }
                    }else{
                        $result["code"] = 1;
                        $result["msg"] = "发布网站没有该上级分类！";
                    }
                }
            }else {
                $result["code"] = 1;
                $result["msg"] = $msg;
            }
            return json($result);
        }
        if(!empty($classification_id)){
            //修改获取详情
            $dataInfo = $InformationClassification->dataInfo("classification_id",$classification_id);
            $this->assign('dataInfo',$dataInfo);
        }
        $classificationList = $InformationClassification->classificationList();
        $classificationList = Menus::arrayToTree(collection($classificationList)->toArray(),0,0,"classification_id");
        $Substation = new Substation();//分站类
        $userSubstationList = $Substation->userList();//获取分站集合
        return view('classificationAdd',[
            'title'                     =>  '添加修改资讯分类',
            'classificationList'        =>  $classificationList,
            'userSubstationList'        =>  $userSubstationList,
            'RadioIs'                   =>  config("IsNoRadio"),//获取Radio配置
        ]);
    }

    //删除资讯分类
    public function classificationDel(){
        $classification_id = input("classification_id/d",0);
        $InformationClassification = new InformationClassification();
        if(!empty($classification_id)){
            if($InformationClassification->checkSave($classification_id,0)) {
                $Information = new Information();
                $informationNumber = $Information->getFieldCount("classification_id",$classification_id);
                if (!$informationNumber) {//查看该分类是否有资讯
                    if ($InformationClassification->del("classification_id",$classification_id,true)) {//查看是否有子级分类
                        $result["code"] = 0;
                        $result["msg"] = "操作成功!";
                    } else {
                        $result["code"] = 10001;
                        $result["msg"] = "操作失败，有子分类的上级分类不能直接删除!";
                    }
                } else {
                    $result["code"] = 10000;
                    $result["msg"] = "该分类下有（" . $informationNumber . "）篇资讯不能直接删除！";
                }
            }else{
                $result["code"] = 20000;
                $result["msg"] = "没有权限操作该网站的内容!";
            }
        }else{
            $result["code"] = 1;
            $result["msg"] = "classification_id参数为空!";
        }
        return json($result);
    }

    //资讯列表
    public function informationList(){
        $keyword = input("keyword/s");
        if(input("?table")) {
            $Information = new Information();
            $result["code"] = 0;
            $result["data"] = $Information->informationList($keyword);
            $result["count"] = $Information->informationListCount($keyword);
            return json($result);
        }
        return view('informationList',[
            'title'                     =>  '资讯列表',
        ]);
    }

    //添加修改资讯
    public function informationAdd(){
        $information_id = input("information_id/d",0);
        $Information = new Information();
        $InformationClassification = new InformationClassification();
        if(request()->isPost()){
            $data = input("post.");
            $msg = $Information->checkData($data, [
                ["information_title", "require", "资讯标题不能为空"],
                ["classification_id", "require", "资讯分类不能为空"],
                ["text_content", "require", "资讯正文内容不能为空"],
                ["substation_id", "require", "请选择发布网站"],
            ]);
            if ($msg === true) {
                if($Information->checkName($data["information_title"],$information_id,$data["substation_id"])){
                    $result["code"] = 1;
                    $result["msg"] = '资讯标题已存在';
                }else{
                    if($InformationClassification->checkParentId($data["classification_id"],$data["substation_id"])) {
                        if($Information->checkSave($information_id,$data["substation_id"])) {
                            //获取资讯分类名称
                            $data["classification_name"] = $InformationClassification->getFieldValue("classification_id", $data["classification_id"], "classification_name");
                            if ($Information->mySave($data,"information_id", $information_id)) {
                                $result["code"] = 0;
                                $result["msg"] = "操作成功！";
                            } else {
                                $result["code"] = 10000;
                                $result["msg"] = "操作失败！";
                            }
                        }else{
                            $result["code"] = 20000;
                            $result["msg"] = "没有权限操作该网站的内容!";
                        }
                    }else{
                        $result["code"] = 1;
                        $result["msg"] = "发布的网站没有该分类！";
                    }
                }
            }else {
                $result["code"] = 1;
                $result["msg"] = $msg;
            }
            return json($result);
        }
        if(!empty($information_id)){
            //修改获取详情
            $dataInfo = $Information->dataInfo("information_id",$information_id);
            $this->assign('dataInfo',$dataInfo);
        }
        //获取资讯分类
        $classificationList = $InformationClassification->classificationList();
        $classificationList = Menus::arrayToTree(collection($classificationList)->toArray(),0,0,"classification_id");
        $Substation = new Substation();//分站类
        $userSubstationList = $Substation->userList();//获取分站集合
        $InformationKeyword = new InformationKeyword();
        $keywordAll = $InformationKeyword->keywordAll();
        return view('informationAdd',[
            'title'                     =>  '添加修改资讯',
            'classificationList'        =>  $classificationList,
            'RadioIs'                   =>  config("IsNoRadio"),//获取Radio配置
            'ueditor'                   =>  true,//开启ue编辑器
            'is_super_admin_group'      =>  $this->admin["group_id"] == config("SuperAdminGroupId") ? true : false,//是否超级管理员
            'userSubstationList'        =>  $userSubstationList,
            'keywordAll'                =>  $keywordAll,
        ]);
    }

    //删除资讯
    public function informationDel(){
        $information_id = input("information_id/d",0);
        $Information = new Information();
        if(!empty($information_id)){
            if($Information->checkSave($information_id,0)) {
                if ($Information->del("information_id", $information_id)) {//查看是否有子级分类
                    $result["code"] = 0;
                    $result["msg"] = "操作成功!";
                } else {
                    $result["code"] = 10000;
                    $result["msg"] = "操作失败!";
                }
            }else{
                $result["code"] = 20000;
                $result["msg"] = "没有权限操作该网站的内容!";
            }
        }else{
            $result["code"] = 1;
            $result["msg"] = "information_id参数为空!";
        }
        return json($result);
    }

    //专题列表
    public function keywordList(){
        $keyword = input("keyword/s");
        if(input("?table")) {
            $InformationKeyword = new InformationKeyword();
            $result["code"] = 0;
            $result["data"] = $InformationKeyword->keywordList($keyword);
            $result["count"] = $InformationKeyword->keywordListCount($keyword);
            return json($result);
        }
        return view('keywordList',[
            'title'                     =>  '专题列表',
        ]);
    }

    //添加修改专题
    public function keywordAdd(){
        $keyword_id = input("keyword_id/d",0);
        $InformationKeyword = new InformationKeyword();
        if(request()->isPost()){
            $data = input("post.");
            $msg = $InformationKeyword->checkData($data, [
                ["keyword_name", "require", "专题不能为空"],
            ]);
            if ($msg === true) {
                if($InformationKeyword->checkFieldOnly("keyword_name",$data["keyword_name"],"keyword_id",$keyword_id)){
                    $result["code"] = 1;
                    $result["msg"] = '专题已存在';
                }else{
                    if($InformationKeyword->checkSave($keyword_id)) {
                        if ($InformationKeyword->mySave($data,"keyword_id", $keyword_id)) {
                            $result["code"] = 0;
                            $result["msg"] = "操作成功！";
                        } else {
                            $result["code"] = 10000;
                            $result["msg"] = "操作失败！";
                        }
                    }else{
                        $result["code"] = 20000;
                        $result["msg"] = "没有权限操他人专题!";
                    }
                }
            }else {
                $result["code"] = 1;
                $result["msg"] = $msg;
            }
            return json($result);
        }
        if(!empty($keyword_id)){
            //修改获取详情
            $dataInfo = $InformationKeyword->dataInfo("keyword_id",$keyword_id);
            $this->assign('dataInfo',$dataInfo);
        }
        return view('keywordAdd',[
            'title'                     =>  '添加修改专题',
            'RadioIs'                   =>  config("IsNoRadio"),//获取Radio配置
        ]);
    }

    //删除专题
    public function keywordDel(){
        $keyword_id = input("keyword_id/d",0);
        $InformationKeyword = new InformationKeyword();
        if(!empty($keyword_id)){
            if($InformationKeyword->checkSave($keyword_id)) {
                if ($InformationKeyword->del("keyword_id", $keyword_id)) {//查看是否有子级分类
                    $result["code"] = 0;
                    $result["msg"] = "操作成功!";
                } else {
                    $result["code"] = 10000;
                    $result["msg"] = "操作失败!";
                }
            }else{
                $result["code"] = 20000;
                $result["msg"] = "没有权限操他人专题!";
            }
        }else{
            $result["code"] = 1;
            $result["msg"] = "keyword_id参数为空!";
        }
        return json($result);
    }

    //设置资讯分类
    public function setInformationClassification(){
        $ids = input("ids/s");
        $Information = new Information();
        $InformationClassification = new InformationClassification();
        if(request()->isPost()){
            $data = input("post.");
            $msg = $Information->checkData($data, [
                ["ids", "require", "设置资讯不能为空"],
                ["classification_id", "require", "资讯分类不能为空"],
            ]);
            if ($msg === true) {
                $data["classification_name"] = $InformationClassification->getFieldValue("classification_id", $data["classification_id"], "classification_name");
                if($Information->setInformationClassification($ids,$data)){
                    $result["code"] = 0;
                    $result["msg"] = "操作成功!";
                } else {
                    $result["code"] = 10000;
                    $result["msg"] = "操作失败!";
                }
            }else {
                $result["code"] = 1;
                $result["msg"] = $msg;
            }
            return json($result);
        }
        //获取资讯分类
        $classificationList = $InformationClassification->classificationList();
        $classificationList = Menus::arrayToTree(collection($classificationList)->toArray(),0,0,"classification_id");
        return view('setInformationClassification',[
            'title'                     =>  '添加修改资讯',
            'classificationList'        =>  $classificationList,
            'ids'                       =>  $ids,
        ]);
    }
}
